package com.study.service.impl;

import com.study.dao.UserRoleDao;
import com.study.dao.UserDao;
import com.study.entity.UserRole;
import com.study.entity.User;
import com.study.entity.Vo;
import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public int addUser(User user) {
        return userDao.insertUser(user);
    }

    @Override
    public int deleteUser(Integer userId) {
        // 删除用户权限信息
        userRoleDao.deleteByUserId(userId);
        return userDao.deleteUser(userId);
    }

    @Override
    public int modifyUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public List<User> selectAllUser() {
        return userDao.selectAllUser();
    }

    @Override
    public User selectUser(Integer userId) {
        return userDao.selectUser(userId);
    }

    /*@Override
    public List<Vo> selectUserRole() {
        return userDao.selectUserRole();
    }*/

    /*@Override
    public List<UserRole> selectUserRole(Integer userId) {
        return userRoleDao.selectUserRole(userId);
    }*/
/*@Override
    public Map<String, List<Object>> selectUserInfos() {
        List<UserInfo> userInfos = userInfoDao.selectAllUserInfo();
        List<Vo> userPrivileges = userPrivilegeDao.selectUserPrivilege();
        Map<String,List<Object>> map = new HashMap<>(2);
        map.put("userInfos", Collections.singletonList(userInfos));
        map.put("userPrivileges", Collections.singletonList(userPrivileges));
        return map;
    }*/
    /*@Override
    public List selectUserInfos() {
        List<UserInfo> userInfos = userInfoDao.selectAllUserInfo();
        List<Vo> userPrivileges = userPrivilegeDao.selectUserPrivilege();
        List list = new ArrayList(2);
        list.add(userInfos);
        list.add(userPrivileges);
        return list;
    }*/

    @Override
    public int contains(String userName, String password) {
        return userDao.contains(userName, password);
    }

    @Override
    public User findUserInfoByName(String loginName) {
        return userDao.selectUserInfoByName(loginName);
    }

    /**
     * Cannot add or update a child row:
     */
    @Override
    public void saveUserPrivilege(Integer userId, String[] roleIdBatches) {
        // 插入用户权限(角色)
        userRoleDao.batchInsert(userId, roleIdBatches);
    }

    /**
     * 查询所有的用户及其具有的角色
     */
    @Override
    public List<User> selectAllUser_role() {
        return userDao.selectAllUser_role();
    }
}
